home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxpcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  20.6 KB  |  689 lines

  1. /* Copyright (C) 1993, 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxpcmap.c,v 1.3 2000/09/19 19:00:39 lpd Exp $ */
  20. /* Pattern color mapping for Ghostscript library */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gsutil.h"        /* for gs_next_ids */
  27. #include "gxfixed.h"
  28. #include "gxmatrix.h"
  29. #include "gxcspace.h"        /* for gscolor2.h */
  30. #include "gxcolor2.h"
  31. #include "gxdcolor.h"
  32. #include "gxdevice.h"
  33. #include "gxdevmem.h"
  34. #include "gxpcolor.h"
  35. #include "gzstate.h"
  36.  
  37. /* Define the default size of the Pattern cache. */
  38. #define max_cached_patterns_LARGE 50
  39. #define max_pattern_bits_LARGE 100000
  40. #define max_cached_patterns_SMALL 5
  41. #define max_pattern_bits_SMALL 1000
  42. uint
  43. gx_pat_cache_default_tiles(void)
  44. {
  45. #if arch_small_memory
  46.     return max_cached_patterns_SMALL;
  47. #else
  48.     return (gs_debug_c('.') ? max_cached_patterns_SMALL :
  49.         max_cached_patterns_LARGE);
  50. #endif
  51. }
  52. ulong
  53. gx_pat_cache_default_bits(void)
  54. {
  55. #if arch_small_memory
  56.     return max_pattern_bits_SMALL;
  57. #else
  58.     return (gs_debug_c('.') ? max_pattern_bits_SMALL :
  59.         max_pattern_bits_LARGE);
  60. #endif
  61. }
  62.  
  63. /* Define the structures for Pattern rendering and caching. */
  64. private_st_color_tile();
  65. private_st_color_tile_element();
  66. private_st_pattern_cache();
  67. private_st_device_pattern_accum();
  68.  
  69. /* ------ Pattern rendering ------ */
  70.  
  71. /* Device procedures */
  72. private dev_proc_open_device(pattern_accum_open);
  73. private dev_proc_close_device(pattern_accum_close);
  74. private dev_proc_fill_rectangle(pattern_accum_fill_rectangle);
  75. private dev_proc_copy_mono(pattern_accum_copy_mono);
  76. private dev_proc_copy_color(pattern_accum_copy_color);
  77. private dev_proc_get_bits_rectangle(pattern_accum_get_bits_rectangle);
  78.  
  79. /* The device descriptor */
  80. private const gx_device_pattern_accum gs_pattern_accum_device =
  81. {std_device_std_body_open(gx_device_pattern_accum, 0,
  82.               "pattern accumulator",
  83.               0, 0, 72, 72),
  84.  {
  85.      /* NOTE: all drawing procedures must be defaulted, not forwarded. */
  86.      pattern_accum_open,
  87.      NULL,
  88.      NULL,
  89.      NULL,
  90.      pattern_accum_close,
  91.      NULL,
  92.      NULL,
  93.      pattern_accum_fill_rectangle,
  94.      gx_default_tile_rectangle,
  95.      pattern_accum_copy_mono,
  96.      pattern_accum_copy_color,
  97.      NULL,
  98.      gx_default_get_bits,
  99.      NULL,
  100.      NULL,
  101.      NULL,
  102.      NULL,
  103.      NULL,
  104.      NULL,
  105.      NULL,
  106.      NULL,
  107.      gx_default_copy_alpha,
  108.      NULL,
  109.      gx_default_copy_rop,
  110.      gx_default_fill_path,
  111.      gx_default_stroke_path,
  112.      gx_default_fill_mask,
  113.      gx_default_fill_trapezoid,
  114.      gx_default_fill_parallelogram,
  115.      gx_default_fill_triangle,
  116.      gx_default_draw_thin_line,
  117.      gx_default_begin_image,
  118.      gx_default_image_data,
  119.      gx_default_end_image,
  120.      gx_default_strip_tile_rectangle,
  121.      gx_default_strip_copy_rop,
  122.      gx_get_largest_clipping_box,
  123.      gx_default_begin_typed_image,
  124.      pattern_accum_get_bits_rectangle,
  125.      NULL,
  126.      NULL,
  127.      NULL,
  128.      gx_default_text_begin,
  129.      gx_default_finish_copydevice
  130.  },
  131.  0,                /* target */
  132.  0, 0, 0, 0            /* bitmap_memory, bits, mask, instance */
  133. };
  134.  
  135. /* Allocate a pattern accumulator, with an initial refct of 0. */
  136. gx_device_pattern_accum *
  137. gx_pattern_accum_alloc(gs_memory_t * mem, client_name_t cname)
  138. {
  139.     gx_device_pattern_accum *adev =
  140.     gs_alloc_struct(mem, gx_device_pattern_accum,
  141.             &st_device_pattern_accum, cname);
  142.  
  143.     if (adev == 0)
  144.     return 0;
  145.     gx_device_init((gx_device *)adev,
  146.            (const gx_device *)&gs_pattern_accum_device,
  147.            mem, true);
  148.     gx_device_forward_fill_in_procs((gx_device_forward *)adev);
  149.     return adev;
  150. }
  151.  
  152. /*
  153.  * Initialize a pattern accumulator.
  154.  * Client must already have set instance and bitmap_memory.
  155.  *
  156.  * Note that mask and bits accumulators are only created if necessary.
  157.  */
  158. private int
  159. pattern_accum_open(gx_device * dev)
  160. {
  161.     gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  162.     const gs_pattern1_instance_t *pinst = padev->instance;
  163.     gs_memory_t *mem = padev->bitmap_memory;
  164.     gx_device_memory *mask = 0;
  165.     gx_device_memory *bits = 0;
  166.     /*
  167.      * The client should preset the target, because the device for which the
  168.      * pattern is being rendered may not (in general, will not) be the same
  169.      * as the one that was current when the pattern was instantiated.
  170.      */
  171.     gx_device *target =
  172.     (padev->target == 0 ? gs_currentdevice(pinst->saved) :
  173.      padev->target);
  174.     int width = pinst->size.x;
  175.     int height = pinst->size.y;
  176.     int code = 0;
  177.     bool mask_open = false;
  178.  
  179.     /*
  180.      * C's bizarre coercion rules force us to copy HWResolution in pieces
  181.      * rather than using a single assignment.
  182.      */
  183. #define PDSET(dev)\
  184.   ((dev)->width = width, (dev)->height = height,\
  185.    /*(dev)->HWResolution = target->HWResolution*/\
  186.    (dev)->HWResolution[0] = target->HWResolution[0],\
  187.    (dev)->HWResolution[1] = target->HWResolution[1])
  188.  
  189.     PDSET(padev);
  190.     padev->color_info = target->color_info;
  191.  
  192.     if (pinst->uses_mask) {
  193.         mask = gs_alloc_struct( mem,
  194.                                 gx_device_memory,
  195.                                 &st_device_memory,
  196.                         "pattern_accum_open(mask)"
  197.                                 );
  198.         if (mask == 0)
  199.         return_error(gs_error_VMerror);
  200.         gs_make_mem_mono_device(mask, mem, 0);
  201.         PDSET(mask);
  202.         mask->bitmap_memory = mem;
  203.         mask->base = 0;
  204.         code = (*dev_proc(mask, open_device)) ((gx_device *) mask);
  205.         if (code >= 0) {
  206.         mask_open = true;
  207.         memset(mask->base, 0, mask->raster * mask->height);
  208.     }
  209.     }
  210.  
  211.     if (code >= 0) {
  212.     switch (pinst->template.PaintType) {
  213.         case 2:        /* uncolored */
  214.         gx_device_set_target((gx_device_forward *)padev, target);
  215.         break;
  216.         case 1:        /* colored */
  217.         bits = gs_alloc_struct(mem, gx_device_memory,
  218.                        &st_device_memory,
  219.                        "pattern_accum_open(bits)");
  220.         if (bits == 0)
  221.             code = gs_note_error(gs_error_VMerror);
  222.         else {
  223.             gs_make_mem_device(bits,
  224.             gdev_mem_device_for_bits(target->color_info.depth),
  225.                        mem, -1, target);
  226.             PDSET(bits);
  227. #undef PDSET
  228.             bits->color_info = target->color_info;
  229.             bits->bitmap_memory = mem;
  230.             code = (*dev_proc(bits, open_device)) ((gx_device *) bits);
  231.             gx_device_set_target((gx_device_forward *)padev,
  232.                      (gx_device *)bits);
  233.         }
  234.     }
  235.     }
  236.     if (code < 0) {
  237.     if (bits != 0)
  238.         gs_free_object(mem, bits, "pattern_accum_open(bits)");
  239.         if (mask != 0) {
  240.         if (mask_open)
  241.         (*dev_proc(mask, close_device)) ((gx_device *) mask);
  242.         gs_free_object(mem, mask, "pattern_accum_open(mask)");
  243.         }
  244.     return code;
  245.     }
  246.     padev->mask = mask;
  247.     padev->bits = bits;
  248.     /* Retain the device, so it will survive anomalous grestores. */
  249.     gx_device_retain(dev, true);
  250.     return code;
  251. }
  252.  
  253. /* Close an accumulator and free the bits. */
  254. private int
  255. pattern_accum_close(gx_device * dev)
  256. {
  257.     gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  258.     gs_memory_t *mem = padev->bitmap_memory;
  259.  
  260.     /*
  261.      * If bits != 0, it is the target of the device; reference counting
  262.      * will close and free it.
  263.      */
  264.     gx_device_set_target((gx_device_forward *)padev, NULL);
  265.     padev->bits = 0;
  266.     if (padev->mask != 0) {
  267.         (*dev_proc(padev->mask, close_device)) ((gx_device *) padev->mask);
  268.         gs_free_object(mem, padev->mask, "pattern_accum_close(mask)");
  269.         padev->mask = 0;
  270.     }
  271.     /* Un-retain the device now, so reference counting will free it. */
  272.     gx_device_retain(dev, false);
  273.     return 0;
  274. }
  275.  
  276. /* Fill a rectangle */
  277. private int
  278. pattern_accum_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  279.                  gx_color_index color)
  280. {
  281.     gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  282.  
  283.     if (padev->bits)
  284.     (*dev_proc(padev->target, fill_rectangle))
  285.         (padev->target, x, y, w, h, color);
  286.     if (padev->mask)
  287.         return (*dev_proc(padev->mask, fill_rectangle))
  288.         ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
  289.      else
  290.         return 0;
  291. }
  292.  
  293. /* Copy a monochrome bitmap. */
  294. private int
  295. pattern_accum_copy_mono(gx_device * dev, const byte * data, int data_x,
  296.             int raster, gx_bitmap_id id, int x, int y, int w, int h,
  297.             gx_color_index color0, gx_color_index color1)
  298. {
  299.     gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  300.  
  301.     if (padev->bits)
  302.     (*dev_proc(padev->target, copy_mono))
  303.         (padev->target, data, data_x, raster, id, x, y, w, h,
  304.          color0, color1);
  305.     if (padev->mask) {
  306.         if (color0 != gx_no_color_index)
  307.         color0 = 1;
  308.         if (color1 != gx_no_color_index)
  309.         color1 = 1;
  310.         if (color0 == 1 && color1 == 1)
  311.         return (*dev_proc(padev->mask, fill_rectangle))
  312.             ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
  313.         else
  314.         return (*dev_proc(padev->mask, copy_mono))
  315.             ((gx_device *) padev->mask, data, data_x, raster, id, x, y, w, h,
  316.              color0, color1);
  317.     } else
  318.         return 0;
  319. }
  320.  
  321. /* Copy a color bitmap. */
  322. private int
  323. pattern_accum_copy_color(gx_device * dev, const byte * data, int data_x,
  324.             int raster, gx_bitmap_id id, int x, int y, int w, int h)
  325. {
  326.     gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  327.  
  328.     if (padev->bits)
  329.     (*dev_proc(padev->target, copy_color))
  330.         (padev->target, data, data_x, raster, id, x, y, w, h);
  331.     if (padev->mask)
  332.         return (*dev_proc(padev->mask, fill_rectangle))
  333.         ((gx_device *) padev->mask, x, y, w, h, (gx_color_index) 1);
  334.     else
  335.         return 0;
  336. }
  337.  
  338. /* Read back a rectangle of bits. */
  339. /****** SHOULD USE MASK TO DEFINE UNREAD AREA *****/
  340. private int
  341. pattern_accum_get_bits_rectangle(gx_device * dev, const gs_int_rect * prect,
  342.                gs_get_bits_params_t * params, gs_int_rect ** unread)
  343. {
  344.     gx_device_pattern_accum *const padev = (gx_device_pattern_accum *) dev;
  345.  
  346.     if (padev->bits)
  347.     return (*dev_proc(padev->target, get_bits_rectangle))
  348.         (padev->target, prect, params, unread);
  349.     return_error(gs_error_Fatal); /* can't happen */
  350. }
  351.  
  352. /* ------ Color space implementation ------ */
  353.  
  354. /* Free all entries in a pattern cache. */
  355. private bool
  356. pattern_cache_choose_all(gx_color_tile * ctile, void *proc_data)
  357. {
  358.     return true;
  359. }
  360. private void
  361. pattern_cache_free_all(gx_pattern_cache * pcache)
  362. {
  363.     gx_pattern_cache_winnow(pcache, pattern_cache_choose_all, NULL);
  364. }
  365.  
  366. /* Allocate a Pattern cache. */
  367. gx_pattern_cache *
  368. gx_pattern_alloc_cache(gs_memory_t * mem, uint num_tiles, ulong max_bits)
  369. {
  370.     gx_pattern_cache *pcache =
  371.     gs_alloc_struct(mem, gx_pattern_cache, &st_pattern_cache,
  372.             "pattern_cache_alloc(struct)");
  373.     gx_color_tile *tiles =
  374.     gs_alloc_struct_array(mem, num_tiles, gx_color_tile,
  375.               &st_color_tile_element,
  376.               "pattern_cache_alloc(tiles)");
  377.     uint i;
  378.  
  379.     if (pcache == 0 || tiles == 0) {
  380.     gs_free_object(mem, tiles, "pattern_cache_alloc(tiles)");
  381.     gs_free_object(mem, pcache, "pattern_cache_alloc(struct)");
  382.     return 0;
  383.     }
  384.     pcache->memory = mem;
  385.     pcache->tiles = tiles;
  386.     pcache->num_tiles = num_tiles;
  387.     pcache->tiles_used = 0;
  388.     pcache->next = 0;
  389.     pcache->bits_used = 0;
  390.     pcache->max_bits = max_bits;
  391.     pcache->free_all = pattern_cache_free_all;
  392.     for (i = 0; i < num_tiles; tiles++, i++) {
  393.     tiles->id = gx_no_bitmap_id;
  394.     /* Clear the pointers to pacify the GC. */
  395.     uid_set_invalid(&tiles->uid);
  396.     tiles->tbits.data = 0;
  397.     tiles->tmask.data = 0;
  398.     tiles->index = i;
  399.     }
  400.     return pcache;
  401. }
  402. /* Ensure that an imager has a Pattern cache. */
  403. private int
  404. ensure_pattern_cache(gs_imager_state * pis)
  405. {
  406.     if (pis->pattern_cache == 0) {
  407.     gx_pattern_cache *pcache =
  408.     gx_pattern_alloc_cache(pis->memory,
  409.                    gx_pat_cache_default_tiles(),
  410.                    gx_pat_cache_default_bits());
  411.  
  412.     if (pcache == 0)
  413.         return_error(gs_error_VMerror);
  414.     pis->pattern_cache = pcache;
  415.     }
  416.     return 0;
  417. }
  418.  
  419. /* Get and set the Pattern cache in a gstate. */
  420. gx_pattern_cache *
  421. gstate_pattern_cache(gs_state * pgs)
  422. {
  423.     return pgs->pattern_cache;
  424. }
  425. void
  426. gstate_set_pattern_cache(gs_state * pgs, gx_pattern_cache * pcache)
  427. {
  428.     pgs->pattern_cache = pcache;
  429. }
  430.  
  431. /* Free a Pattern cache entry. */
  432. private void
  433. gx_pattern_cache_free_entry(gx_pattern_cache * pcache, gx_color_tile * ctile)
  434. {
  435.     if (ctile->id != gx_no_bitmap_id) {
  436.     gs_memory_t *mem = pcache->memory;
  437.     gx_device_memory mdev;
  438.  
  439.     /*
  440.      * We must initialize the memory device properly, even though
  441.      * we aren't using it for drawing.
  442.      */
  443.     gs_make_mem_mono_device(&mdev, mem, NULL);
  444.     if (ctile->tmask.data != 0) {
  445.         mdev.width = ctile->tmask.size.x;
  446.         mdev.height = ctile->tmask.size.y;
  447.         /*mdev.color_info.depth = 1;*/
  448.         pcache->bits_used -= gdev_mem_bitmap_size(&mdev);
  449.         gs_free_object(mem, ctile->tmask.data,
  450.                "free_pattern_cache_entry(mask data)");
  451.         ctile->tmask.data = 0;    /* for GC */
  452.     }
  453.     if (ctile->tbits.data != 0) {
  454.         mdev.width = ctile->tbits.size.x;
  455.         mdev.height = ctile->tbits.size.y;
  456.         mdev.color_info.depth = ctile->depth;
  457.         pcache->bits_used -= gdev_mem_bitmap_size(&mdev);
  458.         gs_free_object(mem, ctile->tbits.data,
  459.                "free_pattern_cache_entry(bits data)");
  460.         ctile->tbits.data = 0;    /* for GC */
  461.     }
  462.     ctile->id = gx_no_bitmap_id;
  463.     pcache->tiles_used--;
  464.     }
  465. }
  466.  
  467. /*
  468.  * Add a Pattern cache entry.  This is exported for the interpreter.
  469.  * Note that this does not free any of the data in the accumulator
  470.  * device, but it may zero out the bitmap_memory pointers to prevent
  471.  * the accumulated bitmaps from being freed when the device is closed.
  472.  */
  473. private void make_bitmap(P3(gx_strip_bitmap *, const gx_device_memory *, gx_bitmap_id));
  474. int
  475. gx_pattern_cache_add_entry(gs_imager_state * pis,
  476.            gx_device_pattern_accum * padev, gx_color_tile ** pctile)
  477. {
  478.     gx_device_memory *mbits = padev->bits;
  479.     gx_device_memory *mmask = padev->mask;
  480.     const gs_pattern1_instance_t *pinst = padev->instance;
  481.     gx_pattern_cache *pcache;
  482.     ulong used = 0;
  483.     gx_bitmap_id id = pinst->id;
  484.     gx_color_tile *ctile;
  485.     int code = ensure_pattern_cache(pis);
  486.  
  487.     if (code < 0)
  488.     return code;
  489.     pcache = pis->pattern_cache;
  490.     /*
  491.      * Check whether the pattern completely fills its box.
  492.      * If so, we can avoid the expensive masking operations
  493.      * when using the pattern.
  494.      */
  495.     if (mmask != 0) {
  496.     int y;
  497.  
  498.     for (y = 0; y < mmask->height; y++) {
  499.         const byte *row = scan_line_base(mmask, y);
  500.         int w;
  501.  
  502.         for (w = mmask->width; w > 8; w -= 8)
  503.         if (*row++ != 0xff)
  504.             goto keep;
  505.         if ((*row | (0xff >> w)) != 0xff)
  506.         goto keep;
  507.     }
  508.     /* We don't need a mask. */
  509.     mmask = 0;
  510.       keep:;
  511.     }
  512.     if (mbits != 0)
  513.     used += gdev_mem_bitmap_size(mbits);
  514.     if (mmask != 0)
  515.     used += gdev_mem_bitmap_size(mmask);
  516.     ctile = &pcache->tiles[id % pcache->num_tiles];
  517.     gx_pattern_cache_free_entry(pcache, ctile);
  518.     while (pcache->bits_used + used > pcache->max_bits &&
  519.        pcache->bits_used != 0    /* allow 1 oversized entry (?) */
  520.     ) {
  521.     pcache->next = (pcache->next + 1) % pcache->num_tiles;
  522.     gx_pattern_cache_free_entry(pcache, &pcache->tiles[pcache->next]);
  523.     }
  524.     ctile->id = id;
  525.     ctile->depth = padev->color_info.depth;
  526.     ctile->uid = pinst->template.uid;
  527.     ctile->tiling_type = pinst->template.TilingType;
  528.     ctile->step_matrix = pinst->step_matrix;
  529.     ctile->bbox = pinst->bbox;
  530.     ctile->is_simple = pinst->is_simple;
  531.     if (mbits != 0) {
  532.     make_bitmap(&ctile->tbits, mbits, gs_next_ids(1));
  533.     mbits->bitmap_memory = 0;    /* don't free the bits */
  534.     } else
  535.     ctile->tbits.data = 0;
  536.     if (mmask != 0) {
  537.     make_bitmap(&ctile->tmask, mmask, id);
  538.     mmask->bitmap_memory = 0;    /* don't free the bits */
  539.     } else
  540.     ctile->tmask.data = 0;
  541.     pcache->bits_used += used;
  542.     pcache->tiles_used++;
  543.     *pctile = ctile;
  544.     return 0;
  545. }
  546. private void
  547. make_bitmap(register gx_strip_bitmap * pbm, const gx_device_memory * mdev,
  548.         gx_bitmap_id id)
  549. {
  550.     pbm->data = mdev->base;
  551.     pbm->raster = mdev->raster;
  552.     pbm->rep_width = pbm->size.x = mdev->width;
  553.     pbm->rep_height = pbm->size.y = mdev->height;
  554.     pbm->id = id;
  555.     pbm->rep_shift = pbm->shift = 0;
  556. }
  557.  
  558. /* Purge selected entries from the pattern cache. */
  559. void
  560. gx_pattern_cache_winnow(gx_pattern_cache * pcache,
  561.   bool(*proc) (P2(gx_color_tile * ctile, void *proc_data)), void *proc_data)
  562. {
  563.     uint i;
  564.  
  565.     if (pcache == 0)        /* no cache created yet */
  566.     return;
  567.     for (i = 0; i < pcache->num_tiles; ++i) {
  568.     gx_color_tile *ctile = &pcache->tiles[i];
  569.  
  570.     if (ctile->id != gx_no_bitmap_id && (*proc) (ctile, proc_data))
  571.         gx_pattern_cache_free_entry(pcache, ctile);
  572.     }
  573. }
  574.  
  575. /* Reload a (non-null) Pattern color into the cache. */
  576. /* *pdc is already set, except for colors.pattern.p_tile and mask.m_tile. */
  577. int
  578. gx_pattern_load(gx_device_color * pdc, const gs_imager_state * pis,
  579.         gx_device * dev, gs_color_select_t select)
  580. {
  581.     gx_device_pattern_accum *adev;
  582.     gs_pattern1_instance_t *pinst =
  583.     (gs_pattern1_instance_t *)pdc->ccolor.pattern;
  584.     gs_state *saved;
  585.     gx_color_tile *ctile;
  586.     gs_memory_t *mem = pis->memory;
  587.     int code;
  588.  
  589.     if (gx_pattern_cache_lookup(pdc, pis, dev, select))
  590.     return 0;
  591.     /* We REALLY don't like the following cast.... */
  592.     code = ensure_pattern_cache((gs_imager_state *) pis);
  593.     if (code < 0)
  594.     return code;
  595.     /*
  596.      * Note that adev is an internal device, so it will be freed when the
  597.      * last reference to it from a graphics state is deleted.
  598.      */
  599.     adev = gx_pattern_accum_alloc(mem, "gx_pattern_load");
  600.     if (adev == 0)
  601.     return_error(gs_error_VMerror);
  602.     gx_device_set_target((gx_device_forward *)adev, dev);
  603.     adev->instance = pinst;
  604.     adev->bitmap_memory = mem;
  605.     code = dev_proc(adev, open_device)((gx_device *)adev);
  606.     if (code < 0)
  607.     goto fail;
  608.     saved = gs_gstate(pinst->saved);
  609.     if (saved == 0) {
  610.     code = gs_note_error(gs_error_VMerror);
  611.     goto fail;
  612.     }
  613.     if (saved->pattern_cache == 0)
  614.     saved->pattern_cache = pis->pattern_cache;
  615.     gs_setdevice_no_init(saved, (gx_device *)adev);
  616.     code = (*pinst->template.PaintProc)(&pdc->ccolor, saved);
  617.     if (code < 0) {
  618.     dev_proc(adev, close_device)((gx_device *)adev);
  619.     /* Freeing the state will free the device. */
  620.     gs_state_free(saved);
  621.     return code;
  622.     }
  623.     /* We REALLY don't like the following cast.... */
  624.     code = gx_pattern_cache_add_entry((gs_imager_state *)pis, adev, &ctile);
  625.     if (code >= 0) {
  626.     if (!gx_pattern_cache_lookup(pdc, pis, dev, select)) {
  627.         lprintf("Pattern cache lookup failed after insertion!\n");
  628.         code = gs_note_error(gs_error_Fatal);
  629.     }
  630.     }
  631. #ifdef DEBUG
  632.     if (gs_debug_c('B')) {
  633.         if (adev->mask)
  634.         debug_dump_bitmap(adev->mask->base, adev->mask->raster,
  635.                   adev->mask->height, "[B]Pattern mask");
  636.     if (adev->bits)
  637.         debug_dump_bitmap(((gx_device_memory *) adev->target)->base,
  638.                   ((gx_device_memory *) adev->target)->raster,
  639.                   adev->target->height, "[B]Pattern bits");
  640.     }
  641. #endif
  642.     /* Free the bookkeeping structures, except for the bits and mask */
  643.     /* data iff they are still needed. */
  644.     dev_proc(adev, close_device)((gx_device *)adev);
  645.     /* Freeing the state will free the device. */
  646.     gs_state_free(saved);
  647.     return code;
  648. fail:
  649.     gs_free_object(mem, adev, "gx_pattern_load");
  650.     return code;
  651. }
  652.  
  653. /* Remap a PatternType 1 color. */
  654. cs_proc_remap_color(gx_remap_Pattern);    /* check the prototype */
  655. int
  656. gs_pattern1_remap_color(const gs_client_color * pc, const gs_color_space * pcs,
  657.             gx_device_color * pdc, const gs_imager_state * pis,
  658.             gx_device * dev, gs_color_select_t select)
  659. {
  660.     gs_pattern1_instance_t *pinst = (gs_pattern1_instance_t *)pc->pattern;
  661.     int code;
  662.  
  663.     pdc->ccolor = *pc;
  664.     if (pinst == 0) {
  665.     /* Null pattern */
  666.     color_set_null_pattern(pdc);
  667.     return 0;
  668.     }
  669.     if (pinst->template.PaintType == 2) {    /* uncolored */
  670.     code = (*pcs->params.pattern.base_space.type->remap_color)
  671.         (pc, (const gs_color_space *)&pcs->params.pattern.base_space,
  672.          pdc, pis, dev, select);
  673.     if (code < 0)
  674.         return code;
  675.     if (pdc->type == gx_dc_type_pure)
  676.         pdc->type = &gx_dc_pure_masked;
  677.     else if (pdc->type == gx_dc_type_ht_binary)
  678.         pdc->type = &gx_dc_binary_masked;
  679.     else if (pdc->type == gx_dc_type_ht_colored)
  680.         pdc->type = &gx_dc_colored_masked;
  681.     else
  682.         return_error(gs_error_unregistered);
  683.     } else
  684.     color_set_null_pattern(pdc);
  685.     pdc->mask.id = pinst->id;
  686.     pdc->mask.m_tile = 0;
  687.     return gx_pattern_load(pdc, pis, dev, select);
  688. }
  689.